﻿using DataStruct;
using MyDiskInfo.DataStruct;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using Utils;

namespace WCFServer
{
    /// <summary>
    /// 客户端服务实现
    /// </summary>
    public class ClientServer : IClientServer
    {
        ChannelFactory<IClientServer> channelFactory;

        public ClientServer()
        {
            CreateChannel();
        }

        /// <summary>
        /// 创建连接客户终端WCF服务的通道
        /// </summary>
        public void CreateChannel()
        {
            try
            {
                string url = "net.pipe://localhost/diskInfoService";
                NetNamedPipeBinding wsHttp = new NetNamedPipeBinding();
                wsHttp.MaxBufferPoolSize = 524288;
                wsHttp.MaxReceivedMessageSize = 2147483647;
                wsHttp.ReaderQuotas.MaxArrayLength = 6553600;
                wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
                wsHttp.ReaderQuotas.MaxBytesPerRead = 6553600;
                wsHttp.ReaderQuotas.MaxDepth = 6553600;
                wsHttp.ReaderQuotas.MaxNameTableCharCount = 6553600;
                wsHttp.SendTimeout = new TimeSpan(0, 10, 0);
                wsHttp.Security.Mode = NetNamedPipeSecurityMode.None;

                channelFactory = new ChannelFactory<IClientServer>(wsHttp, url);
                foreach (OperationDescription op in channelFactory.Endpoint.Contract.Operations)
                {
                    DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;

                    if (dataContractBehavior != null)
                    {
                        dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex, "创建连接客户终端WCF服务的通道失败");
            }
        }

        /// <summary>
        /// 获取硬盘信息
        /// </summary>
        public DiskModel2 GetDiskInfo()
        {
            IClientServer proxy = null;

            try
            {
                if (channelFactory != null)
                {
                    if (channelFactory.State == CommunicationState.Created || channelFactory.State == CommunicationState.Opened)
                    {
                        proxy = channelFactory.CreateChannel();
                        return proxy.GetDiskInfo();
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex, "客户端获取固态硬盘信息失败");
                return null;
            }
            finally
            {
                if (proxy != null)
                {
                    (proxy as ICommunicationObject).Close();
                }
            }
        }

        /// <summary>
        /// 获取CPU信息
        /// </summary>
        public CpuModel GetCpuInfo()
        {
            IClientServer proxy = null;

            try
            {
                if (channelFactory != null)
                {
                    if (channelFactory != null && (channelFactory.State == CommunicationState.Created || channelFactory.State == CommunicationState.Opened))
                    {
                        proxy = channelFactory.CreateChannel();
                        return proxy.GetCpuInfo();
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex, "客户端获取CPU信息失败");
                return null;
            }
            finally
            {
                if (proxy != null)
                {
                    (proxy as ICommunicationObject).Close();
                }
            }
        }
    }
}
